package email.becode.simultaneous_interpretation_android.ui

import android.media.MediaPlayer
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import email.becode.simultaneous_interpretation_android.data.TranslationRecord
import email.becode.simultaneous_interpretation_android.translation.TranslationService
import java.text.SimpleDateFormat
import java.util.*

@Composable
fun HistoryScreen(records: List<TranslationRecord>) {
    var expandedRecordId by remember { mutableStateOf<Long?>(null) }
    var currentlyPlayingId by remember { mutableStateOf<Long?>(null) }
    val context = LocalContext.current
    val mediaPlayer = remember { MediaPlayer() }

    LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        items(records) { record ->
            val isExpanded = expandedRecordId == record.id
            val isPlaying = currentlyPlayingId == record.id

            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = 8.dp)
                    .clickable { 
                        expandedRecordId = if (isExpanded) null else record.id 
                    }
            ) {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                ) {
                    // 显示时间戳
                    Text(
                        text = SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss",
                            Locale.getDefault()
                        ).format(record.timestamp),
                        style = MaterialTheme.typography.labelMedium
                    )

                    Spacer(modifier = Modifier.height(8.dp))

                    // 显示源语言和文本
                    Text(
                        text = when (record.sourceLanguage) {
                            TranslationService.LANGUAGE_CHINESE -> "中文"
                            TranslationService.LANGUAGE_ENGLISH -> "English"
                            TranslationService.LANGUAGE_CANTONESE -> "粤语"
                            else -> record.sourceLanguage
                        } + ": ${record.sourceText}"
                    )

                    if (isExpanded) {
                        Spacer(modifier = Modifier.height(8.dp))

                        // 显示翻译结果
                        record.translations.forEach { (language, text) ->
                            Text(
                                text = when (language) {
                                    TranslationService.LANGUAGE_CHINESE -> "中文翻译: "
                                    TranslationService.LANGUAGE_ENGLISH -> "英文翻译: "
                                    TranslationService.LANGUAGE_CANTONESE -> "粤语翻译: "
                                    else -> "$language: "
                                } + text,
                                modifier = Modifier.padding(vertical = 4.dp)
                            )
                        }

                        Spacer(modifier = Modifier.height(8.dp))

                        // 音频播放控制
                        Row(
                            modifier = Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.Center,
                            verticalAlignment = Alignment.CenterVertically
                        ) {
                            Button(
                                onClick = {
                                    if (isPlaying) {
                                        mediaPlayer.stop()
                                        mediaPlayer.reset()
                                        currentlyPlayingId = null
                                    } else {
                                        try {
                                            mediaPlayer.reset()
                                            mediaPlayer.setDataSource(record.audioFilePath)
                                            mediaPlayer.prepare()
                                            mediaPlayer.start()
                                            currentlyPlayingId = record.id
                                            
                                            mediaPlayer.setOnCompletionListener {
                                                currentlyPlayingId = null
                                            }
                                        } catch (e: Exception) {
                                            e.printStackTrace()
                                        }
                                    }
                                }
                            ) {
                                Text(if (isPlaying) "停止播放" else "播放录音")
                            }
                        }
                    }
                }
            }
        }
    }

    DisposableEffect(Unit) {
        onDispose {
            mediaPlayer.release()
        }
    }
} 